frappe.ui.form.on('Sales Order', {
    refresh(frm) {
        frm.add_custom_button(__('导出图片'), function() {
            frappe.call({
                method: "erpnext_math.utils.export_print_to_image",
                args: {
                    doctype: frm.doc.doctype,
                    name: frm.doc.name,
                    print_format: frm.doc.contract_template || "Sales Contract" // 默认模板或用户选择
                },
                callback: function(r) {
                    if (r.message) {
                        let link = document.createElement('a');
                        link.href = r.message;
                        link.download = frm.doc.name + '.png';
                        link.click();
                    }
                }
            });
        });
    }
});